import { defineMock } from "../base";
import type { FaultReportItem } from "@/api/hmi/fault-report.api";

// 模拟故障报告数据
const mockFaultReports: FaultReportItem[] = [
  {
    faultId: 1,
    reportName: "JMXZLP215_RCD_00020_20250613_090622_822_f",
    reportType: "SOE",
    faultTime: "2025-06-13 09:06:22:822",
    faultStartTime: "2025-06-13 09:06:22:822",
    faultKeepingTime: "0.5s",
    deviceInfo: [
      { name: "设备名称", value: "JMXZLP215" },
      { name: "设备类型", value: "RCD" },
      { name: "设备地址", value: "00020" },
    ],
    tripInfo: [
      {
        name: "SOE",
        time: "2025-06-13 09:06:22:822",
        FaultInfo: {
          name: "过流保护动作",
          value: { "@_InfCode": "001", "#text": "过流保护动作" },
        },
      },
    ],
    digitalStatus: [
      { name: "开关状态", value: "分闸" },
      { name: "保护状态", value: "动作" },
    ],
    digitalEvent: [
      {
        name: "过流保护",
        value: "动作",
        time: "2025-06-13 09:06:22:822",
      },
    ],
    sysSettingValue: [
      { name: "过流定值", value: "100", unit: "A" },
      { name: "过流时间", value: "0.5", unit: "s" },
    ],
    settingValue: [
      { name: "过流保护", value: "投入", unit: "" },
      { name: "过流时间", value: "0.5", unit: "s" },
    ],
    relayEnaInfo: [
      { name: "跳闸继电器", value: "投入" },
      { name: "合闸继电器", value: "退出" },
    ],
  },
  {
    faultId: 2,
    reportName: "JMXZLP215_RCD_00020_20250613_100522_823_f",
    reportType: "SOCILLOGRAPHY",
    faultTime: "2025-06-13 10:05:22:823",
    faultStartTime: "2025-06-13 10:05:22:823",
    faultKeepingTime: "1.2s",
    deviceInfo: [
      { name: "设备名称", value: "JMXZLP215" },
      { name: "设备类型", value: "RCD" },
      { name: "设备地址", value: "00020" },
    ],
    tripInfo: [
      {
        name: "SOCILLOGRAPHY",
        time: "2025-06-13 10:05:22:823",
        FaultInfo: [
          {
            name: "过压保护动作",
            value: { "@_InfCode": "002", "#text": "过压保护动作" },
          },
          {
            name: "欠压保护动作",
            value: { "@_InfCode": "003", "#text": "欠压保护动作" },
          },
        ],
      },
    ],
    digitalStatus: [
      { name: "开关状态", value: "分闸" },
      { name: "保护状态", value: "动作" },
    ],
    digitalEvent: [
      {
        name: "过压保护",
        value: "动作",
        time: "2025-06-13 10:05:22:823",
      },
      {
        name: "欠压保护",
        value: "动作",
        time: "2025-06-13 10:05:22:824",
      },
    ],
    sysSettingValue: [
      { name: "过压定值", value: "120", unit: "V" },
      { name: "欠压定值", value: "80", unit: "V" },
    ],
    settingValue: [
      { name: "过压保护", value: "投入", unit: "" },
      { name: "欠压保护", value: "投入", unit: "" },
    ],
    relayEnaInfo: [
      { name: "跳闸继电器", value: "投入" },
      { name: "合闸继电器", value: "退出" },
    ],
  },
  {
    faultId: 3,
    reportName: "JMXZLP215_RCD_00020_20250613_110422_824_f",
    reportType: "SOE",
    faultTime: "2025-06-13 11:04:22:824",
    faultStartTime: "2025-06-13 11:04:22:824",
    faultKeepingTime: "0.8s",
    deviceInfo: [
      { name: "设备名称", value: "JMXZLP215" },
      { name: "设备类型", value: "RCD" },
      { name: "设备地址", value: "00020" },
    ],
    tripInfo: [
      {
        name: "SOE",
        time: "2025-06-13 11:04:22:824",
        FaultInfo: {
          name: "接地保护动作",
          value: { "@_InfCode": "004", "#text": "接地保护动作" },
        },
      },
    ],
    digitalStatus: [
      { name: "开关状态", value: "分闸" },
      { name: "保护状态", value: "动作" },
    ],
    digitalEvent: [
      {
        name: "接地保护",
        value: "动作",
        time: "2025-06-13 11:04:22:824",
      },
    ],
    sysSettingValue: [
      { name: "接地定值", value: "50", unit: "A" },
      { name: "接地时间", value: "0.8", unit: "s" },
    ],
    settingValue: [
      { name: "接地保护", value: "投入", unit: "" },
      { name: "接地时间", value: "0.8", unit: "s" },
    ],
    relayEnaInfo: [
      { name: "跳闸继电器", value: "投入" },
      { name: "合闸继电器", value: "退出" },
    ],
  },
];

export default defineMock([
  // 分页查询故障报告列表
  {
    url: "fault/1/offset-list",
    method: ["GET"],
    body: ({ query }) => {
      const { offset = 0, limit = 10, reportType, startTime, endTime } = query;

      let filteredData = [...mockFaultReports];

      // 根据报告类型过滤
      if (reportType && reportType !== "all") {
        filteredData = filteredData.filter((item) => item.reportType === reportType);
      }

      // 根据时间范围过滤
      if (startTime && endTime) {
        filteredData = filteredData.filter((item) => {
          const faultTime = new Date(
            item.faultTime.replace(" ", "T").replace(/:(\d+)$/, ".$1")
          ).getTime();
          return (
            faultTime >= new Date(startTime).getTime() && faultTime <= new Date(endTime).getTime()
          );
        });
      }

      const start = parseInt(offset);
      const end = start + parseInt(limit);
      const list = filteredData.slice(start, end);

      return {
        code: "00000",
        data: {
          list,
          total: filteredData.length,
        },
        msg: "获取成功",
      };
    },
  },

  // 获取故障报告详情
  {
    url: "fault/1/:id",
    method: ["GET"],
    body: ({ params }) => {
      const { id } = params;
      const report = mockFaultReports.find((item) => item.faultId === parseInt(id));

      if (!report) {
        return {
          code: "A0001",
          msg: "故障报告不存在",
        };
      }

      return {
        code: "00000",
        data: report,
        msg: "获取成功",
      };
    },
  },

  // 删除故障报告
  {
    url: "fault/1/:id",
    method: ["DELETE"],
    body: ({ params }) => {
      const { id } = params;
      const index = mockFaultReports.findIndex((item) => item.faultId === parseInt(id));

      if (index === -1) {
        return {
          code: "A0001",
          msg: "故障报告不存在",
        };
      }

      mockFaultReports.splice(index, 1);

      return {
        code: "00000",
        msg: "删除成功",
      };
    },
  },

  // 批量删除故障报告
  {
    url: "fault/1/batch-delete",
    method: ["POST"],
    body: ({ body }) => {
      const { ids } = body;

      ids.forEach((id: number) => {
        const index = mockFaultReports.findIndex((item) => item.faultId === id);
        if (index !== -1) {
          mockFaultReports.splice(index, 1);
        }
      });

      return {
        code: "00000",
        msg: "批量删除成功",
      };
    },
  },

  // 下载故障报告
  {
    url: "fault/1/:id/download",
    method: ["GET"],
    body: ({ params }) => {
      const { id } = params;
      const report = mockFaultReports.find((item) => item.faultId === parseInt(id));

      if (!report) {
        return {
          code: "A0001",
          msg: "故障报告不存在",
        };
      }

      // 返回JSON格式的故障报告数据
      return JSON.stringify(report, null, 2);
    },
  },

  // 手动触发故障报告生成
  {
    url: "fault/1/trigger",
    method: ["POST"],
    body: () => {
      return {
        code: "00000",
        msg: "手动触发成功",
      };
    },
  },

  // 检查故障事件
  {
    url: "fault/1/check-event",
    method: ["GET"],
    body: () => {
      return {
        code: "00000",
        data: {
          hasEvent: true,
          eventCount: 3,
          lastEventTime: "2025-06-13 11:04:22:824",
        },
        msg: "检查事件成功",
      };
    },
  },

  // 触发新故障事件
  {
    url: "fault/1/trigger-event",
    method: ["POST"],
    body: () => {
      return {
        code: "00000",
        msg: "触发新事件成功",
      };
    },
  },
]);
